{% extends "../base.html" %}

{% block title %} {% end %}
{% block css %} {% end %}
{% block body %}
<form class="layui-form" action="{{ request.uri }}" method="post" id="editForm">
    {% raw xsrf_form_html() %}
    <input type="hidden" name="rsa_encrypt" value="{{ rsa_encrypt }}">
    <div class="page-form">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input field-username" name="username" lay-verify="required" autocomplete="off" placeholder="请输入用户名" readonly="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">登陆密码</label>
            <div class="layui-input-inline">
                <input type="password" class="layui-input" name="password" lay-verify="password" autocomplete="off" placeholder="******">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系邮箱</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input field-email" name="email" lay-verify="" autocomplete="off" placeholder="请输入邮箱地址">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系手机</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input field-mobile" name="mobile" lay-verify="" autocomplete="off" placeholder="请输入手机号码">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"> </label>
            <div class="layui-input-block">
                <input type="hidden" class="field-id" name="id">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="formSubmit">提交</button>
                <a href="/admin/welcome" class="layui-btn layui-btn-primary ml10"><i class="aicon ai-fanhui"></i>返回</a>
            </div>
        </div>
    </div>
</form>
 {% end %}

{% block javascript %}
<script src="{{ static_url('js/jsencrypt.js') }}" charset="utf-8"></script>
<script type="text/javascript">
var rsa_encrypt = '{{ rsa_encrypt }}'
var public_key = '{{ public_key.replace('\n', '')[26:-24] }}'
var formData = {% raw data_info %}

layui.use(['form'], function() {
    var $ = layui.jquery
    var form = layui.form

    set_form_data(formData)

    // 表单验证
    form.verify({
        username : [/[A-Za-z0-9\u4e00-\u9fa5]{2,40}$/, '用户名必须2到40位字母、数字、汉字组合'],
        // password : [/(.+){8,40}$/, '8到40个任意字符'],
        number : [/^[0-9]*$/, '必须输入数字啊']
    })
    form.on('submit(formSubmit)', function(obj) {
        layer.msg('数据提交中...',{time:50000})
        if (rsa_encrypt=='1' && obj.field.password) {
            // Encrypt with the public key...
            var encrypt = new JSEncrypt()
            encrypt.setPublicKey(public_key)
            var ciphertext = encrypt.encrypt(obj.field.password)
            obj.field.password = ciphertext
        }

        $.ajax({
            type: "POST",
            url: obj.form.action,
            data: obj.field,
            success: function(res) {
                if (res.code==0) {
                    layer.msg('更新成功', {icon: 1, time: 2000}, function(){
                        // var index = parent.layer.getFrameIndex(window.name);
                        // parent.layer.close(index);
                    })
                } else if(res.msg) {
                    layer.msg(res.msg)
                    $(":input[name='username']").focus()
                } else {
                    layer.msg('{{ _('未知错误') }}')
                }
            },
            error: function(xhr){
                console.log(xhr.responseJSON)
                if (xhr.responseJSON && xhr.responseJSON.msg) {
                    layer.msg(xhr.responseJSON.msg)
                } else {
                    layer.msg('{{ _('未知错误') }}')
                }
            }
        })
        return false;
    })
})
</script>
 {% end %}
